package com.warm.base.jdk.thread.realize;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 创建线程的方式
 */
public class CreateThread {
    public static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println(">>> Thread by extents Thread...");
        }
    }

    public static class RunnableTask implements Runnable {

        @Override
        public void run() {
            System.out.println(">>> Thread by implements Runnable...");
        }
    }

    /**
     * 有返回值
     */
    public static class CallerTask implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println(">>> Thread by Callable FutureTask...");
            return "caller Thread";
        }
    }

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();

        RunnableTask runnableTask = new RunnableTask();
        new Thread(runnableTask).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(">>> Thread by implements Runnable -- Anonymous Runnable");
            }
        }).start();

        FutureTask<String> futureTask = new FutureTask<>(new CallerTask());
        new Thread(futureTask).start();
        try {
            String result = futureTask.get();
            System.out.println(result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
